package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @Author:石小俊
 * @Date:2022/11/16 13:40
 * @Version:1.0-SNAPSHOT
 * @Description: 用户登录成功之后才可以访问
 */
public class MainController {
    private User user;
    private Cart cart = new Cart();
    private Scanner sc = new Scanner(System.in);
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");

    public MainController(User user) {
        this.user = user;
    }

    public void showMenu(){
        System.out.println("----------------------------------------");
        System.out.println("欢迎来到网络商城购物系统");
        System.out.println("----------------------------------------");
        System.out.println("1.加入购物车\t\t2.查看购物车");
        System.out.println("----------------------------------------");
        System.out.print("请选择:");
        int selected = sc.nextInt();
        switch (selected){
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            default:
                System.out.println("输入有误");
                break;
        }
    }

    public void addCart(){
        List<Product> products = productService.findAll();
        System.out.println("-------------------------------------------");
        System.out.println("编号\t\t商品名称\t\t商品单价");
        for(Product product : products){
            System.out.println(product.getId()+"\t\t"+product.getName()+"\t\t"+product.getPrice());
        }
        System.out.print("请输入需要加入到购物车的商品编号:");
        Integer id = sc.nextInt();

        try {
            cartService.addCart(id,cart);
            System.out.println("加入购物车成功");
        } catch (UserNotLoginException e) {
            System.out.println("加入失败,"+e.getMessage());
        } catch (ProductNotExistException e) {
            System.out.println("加入失败,"+e.getMessage());
        }
        // 添加完成回到菜单页,让用户继续选择所需要的操作
        showMenu();
    }

    public void showCart(){
        System.out.println("----------------------------------------");
        System.out.println("序号\t\t商品名称\t\t商品数量\t\t商品总价");
        List<Item> items = cart.getItems();
        for(int i = 0; i < items.size(); i++){
            Item item = items.get(i);
            System.out.println((i+1)+"\t\t"+item.getProduct().getName()+"\t\t"+item.getNum()+"\t\t"+item.getPrice());
        }
        System.out.println("----------------------------------------");
        System.out.println("共"+items.size()+"个商品\t\t\t总价:"+cart.getPrice()+"元");
        System.out.println("----------------------------------------");
        System.out.println("请选择操作:1.修改商品数量\t2.删除指定商品的明细\t3.清空购物车\t4.继续购物\t5.生成订单");
        int selected = sc.nextInt();
        switch (selected){
            case 1:
                modifyNum();
                break;
            case 2:
                removeItem();
                break;
            case 3:
                clearCart();
                break;
            case 4:
                showMenu();
                break;
            case 5:
                createOrder();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入");
                break;
        }
    }

    public void modifyNum(){
        System.out.print("请输入需要修改的商品编号:");
        Integer id = sc.nextInt();
        System.out.print("请输入需要修改的商品的数量:");
        Integer num = sc.nextInt();

        try {
            cartService.modifyNum(id,num,cart);
            System.out.println("修改成功");
        } catch (UserNotLoginException e) {
            System.out.println("修改失败,"+e.getMessage());
        } catch (ProductNotExistException e) {
            System.out.println("修改失败,"+e.getMessage());
        }
        showCart();
    }

    public void removeItem(){
        System.out.print("请输入需要移除的明细的商品编号:");
        Integer id = sc.nextInt();

        try {
            cartService.removeCart(id,cart);
            System.out.println("删除成功");
        } catch (UserNotLoginException e) {
            System.out.println("删除失败,"+e.getMessage());
        } catch (ProductNotExistException e) {
            System.out.println("删除失败,"+e.getMessage());
        }
        showCart();
    }

    public void clearCart(){
        cart = new Cart();
        showCart();
    }

    public void createOrder(){
        try {
            String no = orderService.createOrder(cart,user);
            System.out.println("购买成功,您的订单号为:"+no);
            showMenu();
        } catch (UserNotLoginException e) {
            System.out.println("支付失败,"+e.getMessage());
        }
    }
}
